package gk.example.activity;

import gk.example.App;
import gk.example.BaseActivity;
import gk.example.adapter.DateListAdapter;
import gk.example.bean.DateInfo;
import gk.example.bean.User;
import gk.example.util.ErrorCodeUtil;
import gk.example.view.ProgressDialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.listener.FindListener;

import com.example.gkdate.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class DateRecordActivitiy extends BaseActivity implements
		OnClickListener {
	private ImageButton mBtnBack;
	private TextView mTvTitle;
	private PullToRefreshListView mLvDateRecord;
	private DateListAdapter mDateListAdapter;
	private List<DateInfo> mListDate;
	private List<User> mListUser;
	private ImageButton mBtnAnnounceEdit;
	private ProgressDialog mProgressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_record);
		mBtnBack = (ImageButton) findViewById(R.id.btn_back);
		mTvTitle = (TextView) findViewById(R.id.tv_top_title);
		mLvDateRecord = (PullToRefreshListView) findViewById(R.id.lv_date_record);
		mBtnAnnounceEdit = (ImageButton) findViewById(R.id.btn_record_announce_edit);
		mListDate = new ArrayList<DateInfo>();
		mListUser = new ArrayList<User>();
		mProgressDialog = new ProgressDialog();
	}

	@Override
	protected void initEvent() {
		mLvDateRecord.setMode(Mode.PULL_FROM_START);
		mLvDateRecord.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				getRecord();
			}
		});
		mBtnBack.setOnClickListener(this);
		mBtnAnnounceEdit.setOnClickListener(this);
		mTvTitle.setText(getResources().getString(R.string.date_record));
		mProgressDialog.show(getSupportFragmentManager(), "LOADING");
		getRecord();
		mLvDateRecord.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				App.DateInfo = mListDate.get(position - 1);
				finish();
				startActivity(new Intent(DateRecordActivitiy.this,
						AttendActivity.class));
			}
		});

	}

	private void getRecord() {
		BmobQuery<DateInfo> query = new BmobQuery<DateInfo>();
		query.addWhereRelatedTo("DateRecord", new BmobPointer(App.BmobUser));
		query.findObjects(this, new FindListener<DateInfo>() {

			@Override
			public void onSuccess(List<DateInfo> arg0) {
				getUserData(arg0);
			}

			@Override
			public void onError(int arg0, String arg1) {
				ErrorCodeUtil.Errorshow(DateRecordActivitiy.this, arg0, arg1);
				mLvDateRecord.onRefreshComplete();
				mProgressDialog.dismiss();
			}
		});

	}

	private int markForUser = 0;

	/**
	 * 
	 * 描述：获取用户信息
	 * 
	 * @param dateList
	 * @return void
	 */
	private void getUserData(final List<DateInfo> dateList) {
		BmobQuery<User> query = new BmobQuery<User>();
		query.addWhereMatches("objectId", dateList.get(0).getAnnouncer()
				.getObjectId());
		query.findObjects(this, new FindListener<User>() {
			@Override
			public void onSuccess(List<User> arg0) {
				markForUser++;
				mListUser.add(arg0.get(0));
				if (markForUser != dateList.size()) {
					getUserData(dateList);
					return;
				}
				List<User> UserList = new ArrayList<User>();;
				for (int i = 0; i < mListUser.size(); i++) {
					UserList.add(mListUser.get(i));
				}
				markForUser = 0;
				mListDate.clear();
				mListUser.clear();
				for (int i = 0; i < dateList.size(); i++) {
					mListDate.add(dateList.get(i));
					mListUser.add(UserList.get(i));
				}
				mDateListAdapter = new DateListAdapter(
						DateRecordActivitiy.this, mListDate, mListUser);
				mLvDateRecord.setAdapter(mDateListAdapter);
				mLvDateRecord.onRefreshComplete();
				mProgressDialog.dismiss();
			}

			@Override
			public void onError(int arg0, String arg1) {
				ErrorCodeUtil.Errorshow(DateRecordActivitiy.this, arg0, "加载失败");
				mLvDateRecord.onRefreshComplete();
				mProgressDialog.dismiss();
			}

		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			startActivity(new Intent(DateRecordActivitiy.this,
					MainActivity.class));
			finish();
			break;
		case R.id.btn_record_announce_edit:
			startActivity(new Intent(DateRecordActivitiy.this,
					AnnounceActivity.class));
			finish();
			break;
		default:
			break;
		}
	}

}
